package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    /**
     * 上传音视频文件到腾讯云点播服务
     *
     * @param file
     * @return
     */
    @Operation(summary = "上传音视频文件到腾讯云点播服务")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(MultipartFile file) {
        Map<String, String> mapResult = trackInfoService.uploadTrack(file);
        return Result.ok(mapResult);
    }

    /**
     * @param trackInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "新增声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        //1.获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务层保存声音
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }

    /**
     * 获取当前登录声音分页列表
     *
     * @param page           页码
     * @param limit          页大小
     * @param trackInfoQuery 查询条件
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取当前登录声音分页列表")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> getUserTrackByPage(@PathVariable int page, @PathVariable int limit, @RequestBody TrackInfoQuery trackInfoQuery) {
        //1.获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.封装分页查询对象
        trackInfoQuery.setUserId(userId);
        //3.调用业务层进行分页
        Page<TrackListVo> PageInfo = new Page<>(page, limit);
        PageInfo = trackInfoService.getUserTrackByPage(PageInfo, trackInfoQuery);
        return Result.ok(PageInfo);
    }

    /**
     * 根据声音ID查询声音信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据声音ID查询声音信息")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    /**
     * 修改声音信息
     *
     * @param id
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "修改声音信息")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }


    /**
     * 根据声音ID删除声音
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据声音ID删除声音")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    /**
     * 查询当前用户查看专辑声音分页列表
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin(required = false) //如果登录就会获取到用户ID
    @Operation(summary = "查询当前用户查看专辑声音分页列表")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> getUserAlbumTrackPage(@PathVariable Long albumId, @PathVariable Integer page, @PathVariable Integer limit) {
        //1.获取用户ID（可以有，可能没有）
        Long userId = AuthContextHolder.getUserId();
        //2.构建MP分页对象
        Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
        pageInfo = trackInfoService.getUserAlbumTrackPage(pageInfo, userId, albumId);
        return Result.ok(pageInfo);
    }

    /**
     * 根据声音ID，获取声音统计信息
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "根据声音ID，获取声音统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        return Result.ok(trackInfoService.getTrackStatVo(trackId));
    }

    /**
     * 查询当前用户分集购买声音列表
     *
     * @param trackId
     * @return 分集对象包含（名称，价格，数量）
     */
    @GuiGuLogin
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    @Operation(summary = "查询当前用户分集购买声音列表")
    public Result<List<Map<String, Object>>> getUserTrackWaitPayList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> list = trackInfoService.getUserTrackWaitPayList(userId, trackId);
        return Result.ok(list);
    }


    /**
     * 查询当前用户待购声音列表-用于渲染声音结算页面
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @Operation(summary = "查询当前用户待购声音列表-用于渲染声音结算页面")
    @GuiGuLogin
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> getWaitPayTrackInfoList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> list = trackInfoService.getWaitPayTrackInfoList(userId, trackId, trackCount);
        return Result.ok(list);
    }
}

